/**
 * @file Bsp_ProportionalValves.c
 * @author 徐松亮 (5387603@qq.com)
 * @version 1.0
 * @date 2023-09-28
 *
 * @copyright Copyright (c) {2030}  沈阳迈思医疗科技有限公司
 *
 * @brief 驱动层-->比例阀控制程序(实质上为PWM输出控制)
 * @note (仅在h文件描述注释)
 * 	@verbatim
    1.适用芯片:无
    2.移植步骤:无
    3.验证方法:无
    4.使用方法:无
    5.其他说明:无
    @endverbatim
 * @todo (仅在h文件描述待办事项)
 * @par 修改日志:
 * <table>
 * <tr><th>日期   <th>版本 <th>作者  <th>描述
 * <tr><td>2023-09-28 <td>1.0     <td>徐松亮     <td>创建初始版本
 * </table>
 */
/* 私有头部文件 (Includes)-----------------------------------------------------*/
#include <stdlib.h>
#include "Bsp_ProportionalValves.h"
#include "Module_Debug.h"
#include "Bsp.h"
/* 编译警告条件 (Compile warning condition)------------------------------------*/
/* 私有类型定义 (Private typedef)----------------------------------------------*/
/* 私有型宏定义 (Private define)-----------------------------------------------*/
/* 私有变量定义 (Private variables)--------------------------------------------*/
/* 私有函数声明 (Private function prototypes)----------------------------------*/
static void BspProportionalValves_SubFun_Write(BSP_PROPORTIONAL_VALVES_MODULE PVmodule, uint16_t p);
static void BspProportionalValves_Cycle_100ms(void *_tp_interface);
static void BspProportionalValves_Write(void *_tp_interface);
/* 私有函数实体 (Private functions)--------------------------------------------*/
static void BspProportionalValves_SubFun_Write(BSP_PROPORTIONAL_VALVES_MODULE PVmodule, uint16_t p)
{
    switch (PVmodule)
    {
    case BSP_PROPORTIONAL_VALVES_MODULE_ASCO:
        bsp_SetTIMOutPWM(GPIO_PROPORTIONAL_VALVES_GPIO_Port, GPIO_PROPORTIONAL_VALVES_Pin, TIM5, 4, BSP_PROPORTIONAL_VALVES_MODULE_FREQ_ASCO, p);
        break;
    case BSP_PROPORTIONAL_VALVES_MODULE_C921TW20711:
        bsp_SetTIMOutPWM(GPIO_PROPORTIONAL_VALVES_GPIO_Port, GPIO_PROPORTIONAL_VALVES_Pin, TIM5, 4, BSP_PROPORTIONAL_VALVES_MODULE_FREQ_C921TW20711, p);
        break;
    case BSP_PROPORTIONAL_VALVES_MODULE_PARKER:
        bsp_SetTIMOutPWM(GPIO_PROPORTIONAL_VALVES_GPIO_Port, GPIO_PROPORTIONAL_VALVES_Pin, TIM5, 4, BSP_PROPORTIONAL_VALVES_MODULE_FREQ_PARKER, p);
        break;
    default:
        bsp_SetTIMOutPWM(GPIO_PROPORTIONAL_VALVES_GPIO_Port, GPIO_PROPORTIONAL_VALVES_Pin, TIM5, 4, BSP_PROPORTIONAL_VALVES_MODULE_FREQ_C921TW20711, p);
        break;
    }
}
/**
 * @brief   周期执行函数
 * @note    用于Debug测试等
 * @param   None
 * @return  None
 */
static void BspProportionalValves_Cycle_100ms(void *_tp_interface)
{
    BSP_PROPORTIONAL_VALVES_T_INTERFACE *tp_interface = _tp_interface;
    static uint16_t si16 = 0;
    if (tp_interface->t_Private.uc_DebugTestEnable == 1)
    {
        si16 += (20000 / 20);
        if (si16 > 20000)
        {
            si16 = 0;
        }
        BspProportionalValves_SubFun_Write(BSP_PROPORTIONAL_VALVES_MODULE_C921TW20711, abs(si16 - 10000));
    }
    else
    {
        si16 = 0;
    }
}
/**
 * @brief   周期执行函数
 * @note    用于Debug测试等
 * @param   None
 * @return  None
 */
// BUG:应把设置值当做形参
static void BspProportionalValves_Write(void *_tp_interface)
{
    BSP_PROPORTIONAL_VALVES_T_INTERFACE *tp_interface = _tp_interface;
    if (tp_interface->t_Private.uc_DebugTestEnable == 1)
    {
        return;
    }
    BspProportionalValves_SubFun_Write(tp_interface->t_Input.t_Module, tp_interface->t_Input.us_Pwm);
}
/**
 * @brief   初始化函数
 * @note    用于初始化变量
 * @param   None
 * @return  None
 */
void BspProportionalValves_Init(BSP_PROPORTIONAL_VALVES_T_INTERFACE *_tp_interface)
{
    _tp_interface->t_Transplant.fnp_Cycle_100ms = BspProportionalValves_Cycle_100ms;
    _tp_interface->t_Output.fnp_SetPwm = BspProportionalValves_Write;
    _tp_interface->t_Output.fnp_SetPwm(_tp_interface);
}

/**
 * @brief   Debug接口函数
 * @note    开启Debug相关指令后会循环打印
 * @param   OnOff-判断指令进入与退出
 * @return  None
 */

void BspProportionalValves_DebugTestOnOff(uint8_t OnOff, void *_vp_context)
{
    static uint8_t sfirst = 1;
    BSP_PROPORTIONAL_VALVES_T_INTERFACE *tp_interface = _vp_context;
    if (tp_interface == NULL)
    {
        return;
    }
    if (OnOff == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
        if (sfirst == 1)
        {
            sfirst = 0;
            tp_interface->t_Input.fnp_DebugOutStr("Test  Proportional Valves Begin.");
        }
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
        tp_interface->t_Input.fnp_DebugOutStr("Test Proportional Valves End.");
        tp_interface->t_Input.t_Module = BSP_PROPORTIONAL_VALVES_MODULE_C921TW20711;
        tp_interface->t_Input.us_Pwm = 0;
        tp_interface->t_Output.fnp_SetPwm(tp_interface);
    }
}

void BspProportionalValves_TestOnOff(uint8_t OnOff, void *_vp_context)
{
    BSP_PROPORTIONAL_VALVES_T_INTERFACE *tp_interface = (BSP_PROPORTIONAL_VALVES_T_INTERFACE *)_vp_context;
    if (OnOff == true)
    {
        tp_interface->t_Private.uc_DebugTestEnable = 1;
    }
    else
    {
        tp_interface->t_Private.uc_DebugTestEnable = 0;
        tp_interface->t_Input.t_Module = BSP_PROPORTIONAL_VALVES_MODULE_C921TW20711;
        tp_interface->t_Input.us_Pwm = 0;
        tp_interface->t_Output.fnp_SetPwm(tp_interface);
    }
}

/******************* (C) COPYRIGHT 2030 XSLXHN *****END OF FILE****************/
